package com.itheima.springmvc.service.impl;

import com.itheima.springmvc.dao.UserDao;
import com.itheima.springmvc.exception.CustomException;
import com.itheima.springmvc.pojo.User;
import com.itheima.springmvc.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ：sean
 * @date ：Created in 2022/4/16
 * @description ：
 * @version: 1.0
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {
	@Autowired
	private UserDao userDao;

	@Override
	public void add(User user) {
		int insertTag = userDao.insert(user);
		if(insertTag == 0){
			throw new CustomException("插入失败");
		}
	}

	public List<User> queryAll() {
		log.info("[dao层 查询数据....]");
		List<User> users = userDao.queryAll();

		return users;
	}

	@Override
	public User queryUserById(Integer id) {
		User user = userDao.selectUserById(id);
		if (user == null ){
			throw new CustomException("查无此人");
		}
		return user;
	}
}
